package btb.engine;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import btb.net.ANetDossier;
import btb.util.CLogger;

public class CSession extends ANetDossier
{	
	public class NSessionProfile extends NANetProfile
	{
		public static final String NO_CREATION_TIME = "[No creation date-time specified.]";
		public static final String GAME_NOT_STARTED = "This game hasn't started yet.";
		
		private int numPlayers;
		private String creationStamp;
		private String startStamp;
		
		public NSessionProfile()
		{
			super();
			initialize();
		}
		
		public NSessionProfile(CSession _session, String _heading)
		{
			super(_session, _heading);
			initialize();
		}
		
		public NSessionProfile(CSession _session, String _heading, String _timeStamp)
		{
			super(_session, _heading);
			initialize();
			creationStamp = _timeStamp;
		}
		
		protected void initialize()
		{
			numPlayers = 0;
			creationStamp = NO_CREATION_TIME;
			startStamp = GAME_NOT_STARTED;
		}
		
		public void setNumPlayers(int _numPlayers){numPlayers = _numPlayers;}
		public int getNumPlayers(){return numPlayers;}
		public void setCreationStamp(String _dateTimeStamp){creationStamp = _dateTimeStamp;}
		public String getCreationStamp(){return creationStamp;}
		public void setStartStamp(String _dateTimeStamp){startStamp = _dateTimeStamp;}
		public String getStartStamp(){return startStamp;}
				
		public String snapShot()
		{
			String returnStr = "";
			
			returnStr += "Session: " + heading + "\n\n";
			returnStr += "Created at: " + creationStamp + "\n\n";
			returnStr += "[" + numPlayers + "] players have joined.\n\n";
	
			if(startStamp.equals(GAME_NOT_STARTED))
				returnStr += startStamp + "\n\n";
			else
				returnStr += "Game is in progress. It started at: " + startStamp + "\n\n";
			
			return returnStr;
		}
	}
	
	public static final String DEFAULT_USER_DATETIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";
	//public static final String DEFAULT_USER_DATETIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";
	
	private NSessionProfile profile;
	
	private java.util.Date creationTime;
	private java.util.Date startTime;
	private Date parsedDate;
	private SimpleDateFormat format;
	
	public CSession()
	{
		initialize();
		profile = new NSessionProfile(this, NANetProfile.DEFAULT_HEADING);
	}
	
	public CSession(String _heading)
	{
		initialize();
		
		try
		{
			parsedDate = format.parse(creationTime.toString());
			profile = new NSessionProfile(this, _heading, parsedDate.toString());
			// Clog something or other
		}
		catch(ParseException pe)
		{
			CLogger.cond_error("engine/net", "Couldn't properly parse creation Date object. Creating failsafe profile object with no creation time.");
			profile = new NSessionProfile(this, _heading, NSessionProfile.NO_CREATION_TIME);
		}
	}

	protected void initialize()
	{
		creationTime = new Date();
		startTime = null;
		format = new SimpleDateFormat(DEFAULT_USER_DATETIME_PATTERN);
	}

	@Override
	public void setProfile(NANetProfile _profile)
	{
		try
		{
			profile = (NSessionProfile) _profile;
			profile.setOwner(this);
		}
		catch(Exception e)
		{
			CLogger.cond_error("engine/net", "Attempted to set profile object for CSession object; general Exception thrown.");
		}
	}

	@Override
	public NANetProfile getProfile(){return profile;}
}
